
package com.mystery.action;

import com.mystery.pojo.User;
import com.mystery.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


public class UserAction extends ActionSupport implements ModelDriven<User>{

	private User user = new User();

	private List<User> userList;

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	@Autowired
	private UserService userService;
	
	public String login() {
		
		User loginUser = userService.login(user.getUsername(), user.getPassword());
		if(loginUser != null){
			ServletActionContext.getRequest().getSession().setAttribute("user",loginUser);
			return "login_ok";
		}else {
			ServletActionContext.getRequest().setAttribute("error","用户名或密码不正确！");
			return "login_error";
		}
	}

	public String fitnessLogin(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User loginUser = userService.fitnessLogin(username, password);
		if(loginUser != null){
			ServletActionContext.getRequest().getSession().setAttribute("fitness",loginUser);
			return "fitnessLogin_ok";
		}else {
			ServletActionContext.getRequest().setAttribute("error","对不起您不是健身教练！请重新登录！");
			return "fitnessLogin_error";
		}
	}

	public String managerLogin(){
		User loginUser = userService.managerLogin(user.getUsername(), user.getPassword());
		if(loginUser != null){
			userList = userService.list();
			return "managerLogin_ok";
		}else {
			ServletActionContext.getRequest().setAttribute("error","对不起您不是管理员！请重新登录！");
			return "managerLogin_error";
		}
	}
	
	public String addUser() {
		User login = userService.login(user.getUsername(), user.getPassword());
		if(login !=null){
			ServletActionContext.getRequest().setAttribute("error","用户已存在！");
			return "add_error";
		}
		String sex = ServletActionContext.getRequest().getParameter("sex");
		if("man".equals(sex)){
			user.setSex(true);
		}else {
			user.setSex(false);
		}
		int i = userService.addUser(user);
		if(i>0){
			return"add_ok";
		}
		return "add_error";
	}

	public String addFitness() {
		User login = userService.login(user.getUsername(), user.getPassword());
		if(login !=null){
			ServletActionContext.getRequest().setAttribute("error","用户已存在！");
			return "addFitness_error";
		}
		String sex = ServletActionContext.getRequest().getParameter("sex");
		if("man".equals(sex)){
			user.setSex(true);
		}else {
			user.setSex(false);
		}
		int i = userService.addFitness(user);
		if(i>0){
			return"addFitness_ok";
		}
		return "addFitness_error";
	}

	public String reset(){
		User login = userService.login(user.getUsername(), user.getPassword());
		if(login != null){
			String resetPassword = ServletActionContext.getRequest().getParameter("resetPassword");
			int update = userService.update(user.getUsername(), resetPassword);
			if(update > 0){
				return "reset";
			}
			ServletActionContext.getRequest().setAttribute("error","重置失败！");
			return "reset_error";
		}else{
			ServletActionContext.getRequest().setAttribute("error","用户不存在！");
			return "login_error";
		}
	}

	public String search(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		if("".equals(username)){
			userList = userService.list();
		}else {
			userList = userService.search("%"+username+"%");
		}

		return "search";
	}

	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long id = (Long) request.getAttribute("id");
		int delete = userService.delete(id);
		if (delete > 0){
			userList = userService.list();
		}
		return "delete";
	}

	@Override
	public User getModel() {

		return user;
	}

}
